package com.orhon.smartcampus.modules.base.fetcher;

import com.orhon.smartcampus.modules.base.dao.SubjectsDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPeriod;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSubject;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.PeriodMapper;
import com.orhon.smartcampus.modules.base.mm.SubjectMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SubjectFetcher implements DataFetcher {

	@Autowired
	private SubjectsDao subjectsDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		int id = Integer.parseInt(env.getArgument("id").toString());
		String hql = "from Subjects where deleted_at = null and id = "+id;
		Subjects item =  subjectsDao.findId(hql, null);
		if(item!=null) {
			List<GQLPeriod> gqlPeriod = null;
			if (env.getSelectionSet().contains("period")){
				List<Periods> list = item.getPeriod();
				gqlPeriod = PeriodMapper.MAPPER.toPeriodsList(list);
			}
			List<GQLGrades> gqlGrades = null;
			if (env.getSelectionSet().contains("grade")){
				List<Grades> grade = item.getGrade();
				gqlGrades = GradeMapper.MAPPER.toGradeList(grade);
			}
			List<GQLTeacher> gqlTeacher = null;
			if (env.getSelectionSet().contains("teacher")){
				List<TInformation> teacer = item.getTeacher();
				gqlTeacher = TeacherMapper.MAPPER.toTeacherList(teacer);
			}
			GQLSubject subject = SubjectMapper.MAPPER.toSubject(item);
			subject.setPeriod(gqlPeriod);
			subject.setTeacher(gqlTeacher);
			subject.setGrade(gqlGrades);
			return subject;
		}
		return null;
	}


}
